package com.xluomo.notification_listen_service_plugin;

import android.app.Notification;
import android.content.Intent;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

public class NotificationListenService extends NotificationListenerService {

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        handleNotification(sbn, false);
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        handleNotification(sbn, true);
    }

    private void handleNotification(StatusBarNotification notification, boolean isRemove) {
        Intent intent = new Intent(NotificationVars.INTENT);
        intent.putExtra(NotificationVars.PACKAGE, notification.getPackageName());
        intent.putExtra(NotificationVars.ID, notification.getId());
        Bundle extras = notification.getNotification().extras;
        if (extras != null) {
            CharSequence title = extras.getCharSequence(Notification.EXTRA_TITLE);
            CharSequence text = extras.getCharSequence(Notification.EXTRA_TEXT);
            intent.putExtra(NotificationVars.TITLE, title);
            intent.putExtra(NotificationVars.CONTENT, text);
            intent.putExtra(NotificationVars.REMOVE, isRemove);
        }
        sendBroadcast(intent);
    }
}
